import requests

# from js_code import *
import json

def js_from_file(file_name):
    with open(file_name, 'r', encoding='UTF-8') as file:
        result = file.read()
    return result

context = js2py.EvalJs()
js = '''

function base64decode(t) {
        var e, n, r, o, i, u, c, s = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1];
        for (u = t.length,
        i = 0,
        c = ""; i < u; ) {
            do {
                e = s[255 & t.charCodeAt(i++)]
            } while (i < u && -1 === e);
            if (-1 === e)
                break;
            do {
                n = s[255 & t.charCodeAt(i++)]
            } while (i < u && -1 === n);
            if (-1 === n)
                break;
            c += String.fromCharCode(e << 2 | (48 & n) >> 4);
            do {
                if (61 === (r = 255 & t.charCodeAt(i++)))
                    return c;
                r = s[r]
            } while (i < u && -1 === r);
            if (-1 === r)
                break;
            c += String.fromCharCode((15 & n) << 4 | (60 & r) >> 2);
            do {
                if (61 === (o = 255 & t.charCodeAt(i++)))
                    return c;
                o = s[o]
            } while (i < u && -1 === o);
            if (-1 === o)
                break;
            c += String.fromCharCode((3 & r) << 6 | o)
        }
        return c
    }
'''
context.execute(js)
# context.execute(js_from_file('dcd.js'))



def get_mp4_url(aid,vedio_id):
    import js2py
    js = '''

       function base64decode(t) {
               var e, n, r, o, i, u, c, s = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1];
               for (u = t.length,
               i = 0,
               c = ""; i < u; ) {
                   do {
                       e = s[255 & t.charCodeAt(i++)]
                   } while (i < u && -1 === e);
                   if (-1 === e)
                       break;
                   do {
                       n = s[255 & t.charCodeAt(i++)]
                   } while (i < u && -1 === n);
                   if (-1 === n)
                       break;
                   c += String.fromCharCode(e << 2 | (48 & n) >> 4);
                   do {
                       if (61 === (r = 255 & t.charCodeAt(i++)))
                           return c;
                       r = s[r]
                   } while (i < u && -1 === r);
                   if (-1 === r)
                       break;
                   c += String.fromCharCode((15 & n) << 4 | (60 & r) >> 2);
                   do {
                       if (61 === (o = 255 & t.charCodeAt(i++)))
                           return c;
                       o = s[o]
                   } while (i < u && -1 === o);
                   if (-1 === o)
                       break;
                   c += String.fromCharCode((3 & r) << 6 | o)
               }
               return c
           }
       '''
    headers = {
        "authority": "www.dongchedi.com",
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,en-GB;q=0.6",
        "referer": "https://www.dongchedi.com/community/393/quality-2",
        "sec-ch-ua": "\"Chromium\";v=\"116\", \"Not)A;Brand\";v=\"24\", \"Microsoft Edge\";v=\"116\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"macOS\"",
        "sec-fetch-dest": "empty",
        "sec-fetch-mode": "cors",
        "sec-fetch-site": "same-origin",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.0.0"
    }
    url = "https://www.dongchedi.com/motor/pc/common/token"
    params = {
        "aid": aid,
        "app_name": "auto_web_pc",
        "video_id": vedio_id,   # "v02aadg10000ci432bbc77ub0h0ncvg0",
        "format_type": "mp4"
    }
    response = requests.get(url, headers=headers, params=params)
    tokens = response.json()["data"]["play_auth_token"]
    context = js2py.EvalJs()
    context.execute(js)
    result1 = context.base64decode(tokens)
    url1 = "https://vod.bytedanceapi.com/?"+json.loads(result1)["GetPlayInfoToken"]+"&ssl=true"
    response2 = requests.get(url1, headers=headers)
    data3 = response2.json()
    mp4_url =data3["Result"]["Data"].get("PlayInfoList")[-1]["MainPlayUrl"]
    return mp4_url

if __name__ == '__main__':
    get_mp4_url("1839","v03aadg10000cikuburc77ubvpf14l6g")